//
//  PPAccountProfiles.m
//  
//
//  Created by Jinxiao on 9/10/16.
//
//

#import "PPAccountProfiles.h"

@implementation PPAccountProfiles

// Insert code here to add functionality to your managed object subclass
+ (instancetype)existingProfilesForUserID:(PPUserID)userID {
    if(userID.length == 0) {
        return nil;
    }

    PPAccountProfiles *profiles = [PPAccountProfiles find:@{@"userID": userID}];
    if(profiles == nil) {
        profiles = [PPAccountProfiles create];
        profiles.userID = userID;
        [profiles.managedObjectContext saveContext];
    }
    return profiles;
}

- (void)mergeParameters:(NSDictionary *)parameters {
    self.avatar = [parameters validStringForKey:@"avatar"];
//    self.phone = [parameters validStringForKey:@"phone"];
    self.avatarHost = [parameters validStringForKey:@"avatarHost"];
    self.account = [parameters validStringForKey:@"pickpic"];
    self.state = [parameters validStringForKey:@"state"];
    self.city = [parameters validStringForKey:@"city"];
    self.country = [parameters validStringForKey:@"country"];
    self.fans = [parameters validNumberForKey:@"fans"];
    self.follows = [parameters validNumberForKey:@"follows"];
    self.gender = [parameters validNumberForKey:@"gender"];
    self.name = [parameters validStringForKey:@"name"];
    self.relationship = [parameters validNumberForKey:@"relationship"];
    self.signature = [parameters validStringForKey:@"signature"];
    self.birthday = [parameters validStringForKey:@"birthday"];
    self.albums = [parameters validNumberForKey:@"album"];
    self.msgCfgAll = [[parameters validDictionaryForKey:@"msgCfg"] validNumberForKey:@"all"];
    self.msgCfgComment = [[parameters validDictionaryForKey:@"msgCfg"] validNumberForKey:@"comment"];
    self.msgCfgFan = [[parameters validDictionaryForKey:@"msgCfg"] validNumberForKey:@"fan"];
    self.msgCfgLike = [[parameters validDictionaryForKey:@"msgCfg"] validNumberForKey:@"like"];
    self.msgCfgPickpic = [[parameters validDictionaryForKey:@"msgCfg"] validNumberForKey:@"pickpic"];
}

- (NSString *)avatarURLString {
    if(self.avatar.length == 0 || self.avatarHost.length == 0) {
        return nil;
    }

    if([self.avatar hasPrefix:@"http://"]) {
        return self.avatar;
    } else if([self.avatar hasPrefix:@"http:/"]) {
        return [self.avatar stringByReplacingOccurrencesOfString:@"http:/" withString:@"http://"];
    } else if([self.avatar hasPrefix:@"http:"]) {
        return [self.avatar stringByReplacingOccurrencesOfString:@"http:" withString:@"http://"];
    }

    return [NSString stringWithFormat:@"%@%@", self.avatarHost, self.avatar];
}

@end
